package com.fuqi.android.zhongcai.base.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.model.ImageDisplayer;
import com.fuqi.android.zhongcai.base.model.ImageItem;

import java.util.ArrayList;
import java.util.List;

public class ShowImgAdapter extends BaseAdapter {

    private List<ImageItem> pictures = new ArrayList<ImageItem>();
    private Context mContext;

    public ShowImgAdapter(Context context, List<ImageItem> dataList) {
        this.mContext = context;
        this.pictures = dataList;
    }

    public int getCount() {
            return pictures.size();
    }

    public Object getItem(int position) {
        if (pictures != null
                && pictures.size() == Constants.MAX_IMAGE_SIZE) {
            return pictures.get(position);
        }

        else if (pictures == null || position - 1 < 0
                || position > pictures.size()) {
            return null;
        } else {
            return pictures.get(position - 1);
        }
    }

    public long getItemId(int position) {
        return position;
    }

    ViewHolder viewHolder = null;

    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_publish, null);
            viewHolder.imgShow = (ImageView) convertView
                    .findViewById(R.id.item_grid_image_show);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

            final ImageItem item = pictures.get(position);
            ImageDisplayer.getInstance(mContext).displayBmp(viewHolder.imgShow,
                    item.thumbnailPath, item.sourcePath);

        return convertView;
    }

    private void removeImgs() {
        pictures.clear();
    }

    private void removeImg(int location) {
        if (location + 1 <= pictures.size()) {
            pictures.remove(location);
        }
    }


    public class ViewHolder {

        ImageView imgShow;
    }


    public List<ImageItem> getPictures(){


        return pictures;
    }

    public void setPictures(List<ImageItem> pictures) {
        this.pictures = pictures;
    }


}
